﻿using System;
using System.Collections.Generic;
using System.Text;

namespace olc
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                WriteHeader();
                Console.WriteLine("Use '-help' for a list of commands.");
                Environment.Exit(0);
            }
            for (int i = 0; i < args.Length; i++)
            {
                //check the args for certain settings.
                switch (args[i])
                {
                    case "-help":
                        WriteHeader();
                        Console.WriteLine(@"------------------   Options   ------------------");
                        Console.WriteLine(@"-help             : Shows this prompt");
                        Console.WriteLine(@"-<command> -usage : Shows the usage of the specified command");
                        Console.WriteLine(@"-encrypt          : Encrypts the specified file");
                        Console.WriteLine(@"-decrypt          : Decrypts the specified file");
                        Console.WriteLine(@"-update           : Checks for updates to OpenLaunch");
                        Console.WriteLine(@"------------------------------------------------- ");
                        Environment.Exit(0);
                        break;
                    case "-encrypt":
                        try
                        {
                            if (args[i + 1] == null)
                            {
                            }
                        }
                        catch
                        {
                            WriteHeader();
                            Console.WriteLine(@"--------------- Error -----------------");
                            Console.WriteLine("You must specify the flags.");
                            Console.WriteLine("-b : Encrypt Binary");
                            Console.WriteLine("-t : Encrypt Text");
                            Console.WriteLine("Usage: 'olc -encrypt [-b][-t] <filename>'");
                            Console.WriteLine("----------------------------------------");
                            Environment.Exit(0);
                        }
                        if (args[i + 1] == "-b")
                        {
                            try
                            {
                                DarthEncrypt f = new DarthEncrypt();
                                f.PassPhrase = "(*)*(&AS(D&*(@&Y(Q*Y@H!(HR(*#H(*AHD*S^&F(*A^F)";
                                byte[] m = System.IO.File.ReadAllBytes(args[i + 2]);
                                i++;
                                byte[] enc = f.EncryptBytes(m);
                                System.IO.File.WriteAllBytes(args[i], enc);
                                WriteHeader();
                                Console.WriteLine("----------- Now Encrypting -------------");
                                Console.WriteLine("Successful.");
                                Console.WriteLine("----------------------------------------");
                            }
                            catch (Exception ex1)
                            {
                                WriteHeader();
                                Console.WriteLine("----------- Now Decrypting -------------");
                                Console.WriteLine("Error while encrypting:");
                                Console.WriteLine(ex1.Message);
                                Console.WriteLine("----------------------------------------");
                            }
                        }
                        else if (args[i + 1] == "-t")
                        {
                            try
                            {
                                DarthEncrypt f = new DarthEncrypt();
                                f.PassPhrase = "(*)*(&AS(D&*(@&Y(Q*Y@H!(HR(*#H(*AHD*S^&F(*A^F)";
                                string m = System.IO.File.ReadAllText(args[i + 2]);
                                i++;
                                string enc = f.EncryptString(m);
                                System.IO.File.WriteAllText(args[i], enc);
                                WriteHeader();
                                Console.WriteLine("----------- Now Encrypting -------------");
                                Console.WriteLine("Successful.");
                                Console.WriteLine("----------------------------------------");
                            }
                            catch (Exception ex1)
                            {
                                WriteHeader();
                                Console.WriteLine("----------- Now Decrypting -------------");
                                Console.WriteLine("Error while encrypting:");
                                Console.WriteLine(ex1.Message);
                                Console.WriteLine("----------------------------------------");
                            }
                        }
                        else if (args[i + 1] == "-usage")
                        {
                            ShowUsage(args[i]);
                        }
                        else
                        {
                        }
                        Environment.Exit(0);
                        break;
                    case "-decrypt":
                        try
                        {
                            if (args[i + 1] == null)
                            {
                            }
                        }
                        catch
                        {
                            WriteHeader();
                            Console.WriteLine(@"--------------- Error -----------------");
                            Console.WriteLine("You must specify the flags.");
                            Console.WriteLine("-b : Decrypt Binary");
                            Console.WriteLine("-t : Decrypt Text");
                            Console.WriteLine("Usage: 'olc -decrypt [-b][-t] <filename>'");
                            Console.WriteLine("----------------------------------------");
                            Environment.Exit(0);
                        }
                        if (args[i + 1] == "-b")
                        {
                            try
                            {
                                WriteHeader();
                                Console.WriteLine("----------- Now Decrypting -------------");
                                DarthEncrypt v = new DarthEncrypt();
                                Console.WriteLine("Setting passphrase...");
                                v.PassPhrase = "(*)*(&AS(D&*(@&Y(Q*Y@H!(HR(*#H(*AHD*S^&F(*A^F)";
                                Console.WriteLine("Reading file into stream: MODE=READ_BINARY");
                                byte[] l = System.IO.File.ReadAllBytes(args[i + 2]);
                                i++;
                                Console.WriteLine("Decrypting input stream: MODE=DEC_BINARY");
                                byte[] dec = v.DecryptBytes(l);
                                Console.WriteLine("Successful.");
                                System.IO.File.WriteAllBytes(args[i+2], dec);
                                Console.WriteLine("Writing output stream to file " + args[i+2] + ": MODE=WRITE_BINARY");
                                Console.WriteLine("----------------------------------------");
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Error while decrypting:");
                                Console.WriteLine(ex.Message);
                                Console.WriteLine("----------------------------------------");
                            }
                        }
                        else if (args[i + 1] == "-t")
                        {
                            try
                            {
                                WriteHeader();
                                Console.WriteLine("----------- Now Decrypting -------------");
                                DarthEncrypt v = new DarthEncrypt();
                                Console.WriteLine("Setting passphrase...");
                                v.PassPhrase = "(*)*(&AS(D&*(@&Y(Q*Y@H!(HR(*#H(*AHD*S^&F(*A^F)";
                                Console.WriteLine("Reading file into stream: MODE=READ_TEXT");
                                string l = System.IO.File.ReadAllText(args[i + 2]);
                                i++;
                                Console.WriteLine("Decrypting input stream: MODE=DEC_TEXT");
                                string dec = v.DecryptString(l);
                                Console.WriteLine("Writing output stream to file " + args[i+2] + ": MODE=WRITE_TEXT");
                                System.IO.File.WriteAllText(args[i+2], dec);
                                WriteHeader();
                                Console.WriteLine("----------- Now Decrypting -------------");
                                Console.WriteLine("Successful.");
                                Console.WriteLine("----------------------------------------");
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Error while decrypting:");
                                Console.WriteLine(ex.Message);
                                Console.WriteLine("----------------------------------------");
                            }
                        }
                        else if (args[i + 1] == "-usage")
                        {
                            //Show usage
                            ShowUsage(args[i]);
                        }
                        else
                        {
                            WriteHeader();
                            Console.WriteLine(@"--------------- Error -----------------");
                            Console.WriteLine("You must specify the type of decryption with '-t' or '-b'.");
                            Console.WriteLine("----------------------------------------");
                        }
                        Environment.Exit(0);
                        break;
                    default:
                        WriteHeader();
                        Console.WriteLine(@"-------------------   Error   -------------------");
                        Console.WriteLine(@"Unknown Argument '" + args[i] + "'");
                        Console.WriteLine(@"Use the command 'olc -help' to view help.");
                        Console.WriteLine(@"-------------------------------------------------");
                        Environment.Exit(0);
                        break;
                }
            }
        }
        static void WriteHeader()
        {
            //Generate a random number
            Random f = new Random();
            //Get the number
            string m = System.IO.File.ReadAllText(Environment.CurrentDirectory + "/theme");
            //Decrypt our text
            DarthEncrypt gb = new DarthEncrypt();
            gb.PassPhrase = "KASOJLFALSDHJK#@@IUHFK!@KJH@!#KJHFAS";
            string gf = gb.DecryptString(m);
            string[] g = gf.Split('Q');
            int rand = f.Next(0, g.Length - 1);
            //Read our file that has designs in it.
            //Show the Dialog based on the number.
            Console.WriteLine(g[rand]);
        }
        static void ShowUsage(string arg)
        {
            //First we load our usage file.
            string[] m = System.IO.File.ReadAllLines("usage");
            //Now we look for the data
            foreach (string s in m)
            {
                string[] g = s.Split('|');
                if(g[0] == arg)
                {
                    //Match!
                    //Replace \n with newline
                    g[1] = g[1].Replace(@"\n", "\n");
                    //So display to console.
                    WriteHeader();
                    Console.WriteLine("Usage of " + g[0] + ":");
                    Console.WriteLine(g[1]);
                    break;
                }
            }
            Environment.Exit(0);
        }
    }
}
